
package Controller;

import com.mysql.jdbc.Connection;
import com.mysql.jdbc.Statement;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.logging.Level;
import java.util.logging.Logger;


public class Caja {
    public static Statement st;
    public static ResultSet rs, rs1;
     public static int id;
      public static ResultSet filtrarVentas() throws SQLException{
    st = Controller.Conexion.conectar();
    rs = st.executeQuery("select dia,total_efectivo,total_tarjeta,total_gastos from caja");
    
    return rs;
    }
    public static void cargarTabla(String fecha, float total_efectivo, float total_tarjeta) throws SQLException{       
 
            st = Controller.Conexion.conectar();
            rs= st.executeQuery("SELECT id FROM caja");
            if (rs != null && rs.next()) {
                
                rs1 = st.executeQuery("SELECT max(id) FROM caja"/*
                         * "SELECT id FROM ventas ORDER BY 'id' DESC LIMIT 1"
                         */);
                while (rs1.next()) {
                    id = ((Integer) rs1.getObject(1)) + 1;
                   
                }
                
                
                    
                    Statement st2 = (Statement) Controller.Conexion.conectar();
                    st2.executeUpdate("INSERT INTO caja (id, dia,total_efectivo,total_gastos ,total_tarjeta) VALUES (" + id + ",'" + fecha + "'," + total_efectivo + "," + 0 + ","+total_tarjeta+")");
                  

            } else {
                
              
                    
                    Statement st1 = (Statement) Controller.Conexion.conectar();
                    st1.executeUpdate("INSERT INTO caja (id, dia,total_efectivo,total_gastos ,total_tarjeta) VALUES (" + 1 + ",'" + fecha + "'," + total_efectivo + "," + 0 + "," + total_tarjeta+")");
                   
                    
                }
            }
    
    public static void borrarDatos(){
        Statement st1 = (Statement) Controller.Conexion.conectar();
        try {
            st1.executeQuery("TRUNCATE gastos");
        } catch (SQLException ex) {
            Logger.getLogger(Caja.class.getName()).log(Level.SEVERE, null, ex);
        }
    
    
    }
    
    public static void insertGastoEnTabla(float gasto, String fecha) throws SQLException{
          
                    Statement st1 = (Statement) Controller.Conexion.conectar();
                    st1.executeUpdate("UPDATE caja SET total_gastos =" + gasto + "WHERE dia = '" + fecha + "'");
                   
    
    }
        
        

}
